package com.topisv.tms.task;

import java.text.MessageFormat;
import java.util.List;

import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.util.HtmlUtils;

import com.topisv.tms.entity.ViewLogisticsCompanyNO;
import com.topisv.tms.service.ViewLogisticsCompanyServics;
import com.topisv.tms.service.task.ScheduledTaskService;
import com.topisv.tms.utils.io.ReaderXML;
import com.topisv.tms.utils.mail.MailSenderInfo;
import com.topisv.tms.utils.mail.SimpleMailSender;

@Component
@Lazy(false)
public class MailNoEarly {
	protected Logger log = LoggerFactory.getLogger(getClass());
	@Autowired
	private ViewLogisticsCompanyServics viewLogisticsCompanyService;
	@Autowired
	private ScheduledTaskService scheduledTaskService;
	/**
	 * 定时任务,每天7点发送邮件
	 */
	@Scheduled(cron = "0 0 7 * * *")  
	public void execute() {
		try {
			try {
				scheduledTaskService.lock();
			} catch (Exception e) {
			}
			boolean rs = scheduledTaskService.check();
			if (rs) {
				List<ViewLogisticsCompanyNO> mailNoList = viewLogisticsCompanyService.findMailNo();
				if(mailNoList.size()!=0){
					log.info("--------------开始定时发送邮件---------");
					//sendMail(buildMailContent(mailNoList));
				}
			}
		} catch (Exception e) {
			log.info("--------------发送失败---------");
			e.printStackTrace();
		}
	}
	
	private String buildMailContent(List<ViewLogisticsCompanyNO> contents) {
		Element root = ReaderXML.read("/template/mailNo.xml");
		Element bodyElement = root.element("html").element("body");
		//处理1:
		Element spanElement = bodyElement.element("div");
		spanElement.setText(MessageFormat.format(spanElement.getText(),""));
		//处理2：
		Element trElement = bodyElement.element("table").element("tbody").element("tr");
		String contentText = trElement.asXML();
		int index = 0;
		StringBuilder sb = new StringBuilder();
		for (ViewLogisticsCompanyNO mail : contents) {
			sb.append(MessageFormat.format(contentText, ++index, mail.getGroupName(),mail.getFullName(),mail.getUnuse()));
		}
		Element tbodyElement = trElement.getParent();
		tbodyElement.remove(trElement);
		tbodyElement.setText(sb.toString());
		return HtmlUtils.htmlUnescape(root.element("html").asXML());
	}
	
	public void sendMail(String content) {
		MailSenderInfo mailInfo = buildMailInfo(content);
		SimpleMailSender.sendHtmlMail(mailInfo);
	}
	
	
	private MailSenderInfo buildMailInfo(String content) {
		MailSenderInfo mailInfo = new MailSenderInfo();

		mailInfo.setMailServerHost("smtp.exmail.qq.com");
		mailInfo.setMailServerPort("587");
		mailInfo.setValidate(true);

		mailInfo.setUserName("operation@paiu.com.cn");
		mailInfo.setPassword("paiu2014");

		mailInfo.setFromAddress("operation@paiu.com.cn");
		
		String [] email = {"wangzhiqiang@paiu.com.cn","zhangnan@paiu.com.cn",
				"luoyanhong@paiu.com.cn",
				"huangchuang@paiu.com.cn","tulimei@paiu.com.cn","shangtao@paiu.com.cn",
				"yinguifeng@paiu.com.cn","liurong@paiu.com.cn","yiwei@paiu.com.cn",
				"chengxiaobing@paiu.com.cn","chaojiangtao@paiu.com.cn","kuangzhengquan@paiu.com.cn","yuduoli@paiu.com.cn","yangzhuo@paiu.com.cn"};
		mailInfo.setToAddress(email);
		mailInfo.setCcAddress(new String[]{"chenyue@paiu.com.cn","hewuxiang@paiu.com.cn"});
		mailInfo.setSubject("【重要】电子面单数量不足");
		mailInfo.setContent(content);
		return mailInfo;
	}
}
